package com.coderman.business.service.imp;

import com.coderman.business.converter.GradeConverter;
import com.coderman.business.mapper.GradeMapper;
import com.coderman.business.service.IGradeService;
import com.coderman.business.service.base.AbstractServiceImpl;
import com.coderman.common.error.SystemCodeEnum;
import com.coderman.common.error.SystemException;
import com.coderman.common.model.student.Grade;
import com.coderman.common.vo.student.GradeVO;
import com.coderman.common.vo.system.PageVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 年级业务层实现
 * @author  文晓平
 * @Date 2023-1-9
 * @version 1.0
 */
@Service
public class GradeServiceImpl extends AbstractServiceImpl<GradeMapper, Grade, Integer> implements IGradeService {
    @Autowired
    private GradeMapper gradeMapper;
    @Override
    public PageVO<Grade> findList(Integer pageNum, Integer pageSize, GradeVO gradeVO) {
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Grade.class);
        String gradeName = gradeVO.getGradeName();
        Example.Criteria criteria = o.createCriteria();
        if(!StringUtils.isEmpty(gradeName)){
            criteria.andLike("gradeName","%"+gradeName+"%");
        }
        List<Grade> gradeList= gradeMapper.selectByExample(o);
        PageInfo<Grade> info=new PageInfo<>(gradeList);
        return new PageVO<>(info.getTotal(),gradeList);
    }

    /**
     * 添加年级
     *
     * @param gradeVO
     */
    @Override
    public void add(GradeVO gradeVO) {
        Grade grade = new Grade();
        BeanUtils.copyProperties(gradeVO,grade);
        gradeMapper.insert(grade);
    }

    /**
     * 编辑年级
     *
     * @param id
     * @return
     */
    @Override
    public GradeVO edit(Integer id) throws SystemException {
        Grade grade = gradeMapper.selectByPrimaryKey(id);
        if(grade==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的年级不存在");
        }
        GradeVO gradeVO = GradeConverter.converterToGradeVO(grade);
        return gradeVO;
    }

    /**
     * 更新年级
     *
     * @param id
     * @param gradeVO
     */
    @Override
    public void update(Integer id, GradeVO gradeVO) throws SystemException {
        Grade dbGrade = gradeMapper.selectByPrimaryKey(id);
        if(dbGrade==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的年级信息不存在");
        }
        Grade grade = new Grade();
        BeanUtils.copyProperties(gradeVO,grade);
        grade.setGradeId(id);
        gradeMapper.updateByPrimaryKeySelective(grade);
    }

    /**
     * 删除年级
     *
     * @param id
     */
    @Override
    public void delete(Integer id) throws SystemException {
        Grade grade = gradeMapper.selectByPrimaryKey(id);
        if(grade==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的年级信息不存在");
        }
        gradeMapper.deleteByPrimaryKey(id);
    }

    /**
     * 所有年级信息
     *
     * @return
     */
    @Override
    public List<GradeVO> findAllVO() {
        List<Grade> grades = gradeMapper.selectAll();
        //转vo
        List<GradeVO> gradeVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(grades)) {
            for (Grade grade : grades) {
                GradeVO g = new GradeVO();
                BeanUtils.copyProperties(grade, g);
                Example o = new Example(Grade.class);
                o.createCriteria().andEqualTo("gradeId",grade.getGradeId());
                gradeVOS.add(g);
            }
        }
        return gradeVOS;
    }

    /**
     * 全部年级信息
     *
     * @return
     */
    @Override
    public List<Grade> findAll() {
        return gradeMapper.selectAll();
    }
}
